package com.tear.language.node;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import ognl.Ognl;

import org.apache.commons.lang.StringUtils;

import com.tear.language.utils.Attrs;

/**
	打印节点
*/
public class PrintNode extends BaseNode {

	@Override
	public boolean parse(Map<String, Object> currParams,Map<String, Object> globalParams, JSONObject obj) throws Exception {
		String name = obj.optString(Attrs.NAME);
		String target = obj.optString(Attrs.TARGET);
		Map<String, Object> allParams = getAllParams(currParams,globalParams);
		if(StringUtils.isNotEmpty(name)) {
			Object value = null;
			if(StringUtils.isEmpty(target)) {
				value = Ognl.getValue(name, allParams);
			} else {
				Map map = (Map)allParams.get(target);
				value = Ognl.getValue(name, map);
			}
			List<Object> consoles = (List<Object>)globalParams.get(Attrs.CONSOLE_VAR);
			if(consoles == null) {
				consoles = new ArrayList<Object>();
				consoles.add(value);
				globalParams.put(Attrs.CONSOLE_VAR, consoles);
			} else {
				consoles.add(value);
			}
		}
		return true;
	}

}
